package com.yogurt.edu.controller;


import com.yogurt.edu.entity.Chapter;
import com.yogurt.edu.entity.chapter.ChapterVideoVo;
import com.yogurt.edu.service.ChapterService;
import com.yogurt.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author yogurt
 * @since 2020-07-10
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class ChapterController {
    @Autowired
    private ChapterService chapterService;

    //查询所属课程的所有的章节和小节
    @GetMapping("/findChapterAndVideo/{courseId}")
    public Result findAllChapterAndVideo(@PathVariable String courseId){
        //根据课程id查询所有的章节和小节
        List<ChapterVideoVo> list = chapterService.findChapterAndVideoByCourseId(courseId);
        return Result.ok().data("list",list);
    }

    //添加课程章节
    @PostMapping("/addChapter")
    public Result addChapter(@RequestBody Chapter chapter){
        chapterService.save(chapter);
        return Result.ok();
    }

    //根据id查询课程章节
    @GetMapping("/findChapterById/{chapterId}")
    public Result findChapterById(@PathVariable String chapterId){
        Chapter chapter = chapterService.getById(chapterId);
        if (chapter == null){
            return Result.error();
        }else {
            return Result.ok().data("chapter",chapter);
        }
    }

    //修改课程章节
    @PutMapping("/updateChapter")
    public Result updateChapter(@RequestBody Chapter chapter){
        boolean flag = chapterService.updateById(chapter);
        if (flag){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    //根据id删除课程章节
    @DeleteMapping("/deleteById/{chapterId}")
    public Result deleteChapterById(@PathVariable String chapterId){
        boolean flag = chapterService.deleteById(chapterId);
        if (flag){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

}

